//
//  MineViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 20.04.25.
//

import Foundation
class MineViewModel : KoiAccountingBaseViewModel {
    
    @Published var achivevmentDay: String = ""
    @Published var achivevmentTotalNumber: String = ""
    
    private let detailDao = KoiAccountingDB.standard.getDetailDao()
    
    func loadData() {
        asyncBackground {
            let number = self.detailDao.achievementTotalNumber(uid: self.uid)
            let daySecond = self.detailDao.achievementDay(uid: self.uid)
            
            self.asyncMain {
                self.achivevmentTotalNumber = String(number)
                
                if daySecond == 0 {
                    self.achivevmentDay = "0"
                } else {
                    let date = Date(timeIntervalSince1970: daySecond)
                    let now = Date()
                    let calendar = Calendar.current
                    let diff = calendar.dateComponents([.day], from: date, to: now)
                    let day = diff.day ?? 1
                    self.achivevmentDay = String(day)
                }
            }
        }
    }
}
